01. Introduction to Using Objects
Introduction
A Closer Look at the Maze App
When you run the Maze application, there is a lot more going on than meets the eye. Behind the scenes, the Maze app consists of a larger system of interconnected objects.
Internally, the Maze app is composed of a system of objects.
The Robot Object
You had a chance to interact with one object already—the Robot object—and you sent it messages to move around the maze.
The Robot object is composed of its data and associated functionality.
Objects like the Robot allow us to group related pieces of data. Once the data is grouped, we can more easily interact with it. For example, with the Robot object we can store how the robot looks and operates. Then, to interact with it, we send it instructions such as moveUp and moveDown. We’ll talk more about objects later, but for now remember that objects are capable of encapsulating data (“what it is composed of”) and functionality (“how it operates”).
Structs
In addition to the Robot object, the Maze app creates another special kind of object called a struct. Specifically, each time you sent instructions to the Robot object, a Move struct was created.
When the robot is instructed to move, it creates a Move struct which directs its movement.
A struct (short for “structure”) is an object, like the Robot, that can encapsulate data and functionality. However, objects like the Robot tend to have more functionality than structs, which tend to be used primarily to store data. In the Maze app, the Robot interacts with the Move structs to carry out its various functions. But, the Move struct doesn’t have any real functionality of its own.
The Move Struct
As its name suggests, a Move struct is used by the Maze app to tell the robot object how to move. For example, a Move struct can contain data to move the robot one space to the left. It could also contain data to move the robot one space up or down.
A Move struct models a change in the robot's position.
We will talk more about the Move struct later, but what is important to realize is that the maze, the robot, and the moves are all objects. And, as you write code and build applications, you’ll find that many problems can be modeled with objects.
Reflect: Objects in the Maze App
QUESTION:
Based on the image above and what you've seen about the Maze app, what do you think is the difference between a "Maze Actor" and a "Maze Item"?
ANSWER:
From the diagram above, we see that the Robot is related to a Maze Actor and the Star is related to a Maze Item. Also, we've seen that the Robot can move around the maze whereas the Star stays fixed at a location. This might suggest that Maze Actors are objects that can move about the maze and Maze Items are objects that do not move about the maze.